package com.byx.chatclient.widget;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byx.chatclient.R;
import com.byx.chatclient.adapter.PopupMenuAdapter;
import com.byx.chatclient.bean.PopupMenuItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * PopupMenu
 *
 * @author Superb
 * @date 2021/4/24 16:48
 */
public class PopupMenu {
    private final Activity mContext;
    private PopupWindow mPopupWindow;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.popup_menu_item_list)
    protected RecyclerView mRecyclerView;
    private View root;
    private final PopupMenuAdapter mPopupMenuAdapter;
    private final List<PopupMenuItem> mItemList;

    private int popHeight = RecyclerView.LayoutParams.WRAP_CONTENT;
    private int popWidth = RecyclerView.LayoutParams.WRAP_CONTENT;
    private static final int DEFAULT_ANIM_STYLE = R.style.PopupMenuAnim;
    private int animationStyle;
    private final float alpha = 0.75f;
    private View mAnchor;
    private int mOffX,mOffY;
    private OnMenuClosedListener mOnMenuClosedListener;

    public PopupMenu(Activity context) {
        mContext = context;
        root = LayoutInflater.from(context).inflate(R.layout.layout_popup_menu,null);
        ButterKnife.bind(this,root);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        mItemList = new ArrayList<>();
        mPopupMenuAdapter = new PopupMenuAdapter();
    }

    private PopupWindow getPopupWindow(){
        mPopupWindow = new PopupWindow(mContext);
        mPopupWindow.setContentView(root);
        mPopupWindow.setHeight(popHeight);
        mPopupWindow.setWidth(popWidth);

        mPopupWindow.setAnimationStyle(animationStyle <= 0 ? DEFAULT_ANIM_STYLE : animationStyle);

        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setOnDismissListener(() -> {
            setBackgroundAlpha(alpha, 1f, 300);
            if (mOnMenuClosedListener != null){
                mOnMenuClosedListener.onMenuClosed();
            }
        });

        mPopupMenuAdapter.addData(mItemList);
        mRecyclerView.setAdapter(mPopupMenuAdapter);
        return mPopupWindow;
    }

    public PopupMenu setHeight(int height){
        if (height <= 0 && height != RecyclerView.LayoutParams.MATCH_PARENT
                && height != RecyclerView.LayoutParams.WRAP_CONTENT){
            this.popHeight = RecyclerView.LayoutParams.WRAP_CONTENT;
        }else {
            this.popHeight = height;
        }
        return this;
    }

    public PopupMenu setWidth(int width){
        if (width <= 0 && width != RecyclerView.LayoutParams.MATCH_PARENT){
            this.popWidth = RecyclerView.LayoutParams.WRAP_CONTENT;
        }else {
            this.popWidth = width;
        }
        return this;
    }

    /**
     * 添加单个菜单
     * @param item
     * @return
     */
    public PopupMenu addMenuItem(PopupMenuItem item){
        mItemList.add(item);
        return this;
    }

    /**
     * 添加多个菜单
     * @param list
     * @return
     */
    public PopupMenu addMenuList(List<PopupMenuItem> list){
        mItemList.addAll(list);
        return this;
    }

    /**
     * 设置动画
     * @param style
     * @return
     */
    public PopupMenu setAnimationStyle(int style){
        this.animationStyle = style;
        return this;
    }

    public PopupMenu setOnMenuItemClickListener(OnMenuItemClickListener listener){
        mPopupMenuAdapter.setOnMenuItemClickListener(listener);
        return this;
    }

    public PopupMenu setOnMenuClosedListener(OnMenuClosedListener onMenuClosedListener) {
        this.mOnMenuClosedListener = onMenuClosedListener;
        return this;
    }

    public View getAnchor() {
        return mAnchor;
    }

    public PopupMenu setLocation(View anchor){
        this.mAnchor = anchor;
        return this;
    }

    public PopupMenu setLocation(View anchor, int offX, int offY){
        this.mAnchor = anchor;
        this.mOffX = offX;
        this.mOffY = offY;
        return this;
    }

    public PopupMenu showAsDropDown(){
        showAsDropDown(this.mAnchor, this.mOffX, this.mOffY);
        return this;
    }

    public PopupMenu showAsDropDown(View anchor){
        showAsDropDown(anchor, 0, 0);
        return this;
    }

    private PopupMenu showAsDropDown(View anchor, int xoff, int yoff){
        if (mPopupWindow == null){
            getPopupWindow();
        }
        if (!mPopupWindow.isShowing()) {
            mPopupWindow.showAsDropDown(anchor, xoff, yoff);
            setBackgroundAlpha(1f, alpha, 240);
        }
        return this;
    }

    private void setBackgroundAlpha(float from, float to, int duration) {
        final WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        ValueAnimator animator = ValueAnimator.ofFloat(from, to);
        animator.setDuration(duration);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                lp.alpha = (float) animation.getAnimatedValue();
                mContext.getWindow().setAttributes(lp);
            }
        });
        animator.start();
    }

    public void dismiss(){
        if (mPopupWindow != null && mPopupWindow.isShowing()){
            mPopupWindow.dismiss();
        }
    }

    public interface OnMenuItemClickListener{
        void onMenuItemClick(int position);
    }

    public interface OnMenuClosedListener{
        void onMenuClosed();
    }
}
